package com.itheima.config;


import com.itheima.interceptor.ProjectInterceptor;
import com.itheima.interceptor.ProjectInterceptor2;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan({"com.itheima.controller","com.itheima.config"})
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
    //静态资源放行
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
    }

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //当然也可将ProjectInterceptor维护成一个bean对象，然后注入使用
        //1.注册拦截器
        registry.addInterceptor(new ProjectInterceptor())
        //2.指定拦截器拦截规则 /books/**：表示匹配一切以books开头的路径资源，包含books路径本身
                //**:匹配无限层级的url地址，而*仅仅匹配下一次：/books/* --->eg:/books/info/aaa就匹配不到
        .addPathPatterns("/books/**")
        //3.指定不拦截的资源，比如：注册或者登录的url地址，是无需被拦截处理
         .excludePathPatterns("/books/info");
        //1.注册拦截器
        registry.addInterceptor(new ProjectInterceptor2()).addPathPatterns("/books/**").excludePathPatterns("/books/info");
    }
}